﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using CSP.Common.CustomException;
using System.Web;

namespace CSP.Common.CacheManage
{
    public class LocalCachedProvider : ICacheProvider
    {
        private Cache _cache;

        public LocalCachedProvider()
        {
            _cache = HttpRuntime.Cache;
        }

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T">任意的对象类型</typeparam>
        /// <param name="key">根据Key值查找对象</param>
        /// <returns></returns>
        public T GetData<T>(string key)
        {
            ExceptionFactory.StringNullorEmptyException(key);
            object result = _cache.Get(key);
            if (result == null)
            {
                return default(T);
            }
            return (T)result;
        }

        /// <summary>
        /// 判断缓存对象是否存在
        /// </summary>
        /// <param name="key">根据Key值查找对象</param>
        /// <returns></returns>
        public bool IsExists(string key)
        {
            ExceptionFactory.StringNullorEmptyException(key);

            var data = _cache.Get(key);
            if (data == null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 添加缓存对象,如果存在将替换
        /// </summary>
        /// <typeparam name="T">任意的对象类型</typeparam>
        /// <param name="key">指定存储的key</param>
        /// <param name="value">指定存储的对象</param>
        /// <returns></returns>
        public bool AddData(string key, object value)
        {
            ExceptionFactory.StringNullorEmptyException(key);
            ExceptionFactory.ObjectNullException(value);

            _cache.Insert(key, value);
            return true;
        }

        /// <summary>
        /// 添加缓存对象,如果存在将替换
        /// </summary>
        /// <typeparam name="T">任意的对象类型</typeparam>
        /// <param name="key">指定存储的key</param>
        /// <param name="value">指定存储的对象</param>
        /// <param name="absoluteExpiration">所添加对象将到期并被从缓存中移除的时间。</param>
        /// <param name="dependenceKeys">依赖的缓存键</param>
        /// <returns></returns>
        public bool AddData(string key, object value, DateTime absoluteExpiration, string[] dependenceKeys = null)
        {
            ExceptionFactory.StringNullorEmptyException(key);
            ExceptionFactory.ObjectNullException(value);

            CacheDependency cacheDepends = null;
            if (dependenceKeys != null && dependenceKeys.Length != 0)
            {
                cacheDepends = new CacheDependency(null, dependenceKeys);
            }

            _cache.Insert(key, value, cacheDepends, absoluteExpiration, Cache.NoSlidingExpiration);
            return true;
        }

        /// <summary>
        /// 添加缓存对象,如果存在将替换
        /// </summary>
        /// <typeparam name="T">任意的对象类型</typeparam>
        /// <param name="key">指定存储的key</param>
        /// <param name="value">指定存储的对象</param>
        /// <param name="slidingExpiration">最后一次访问所添加对象时与该对象到期时之间的时间间隔</param>
        /// <param name="dependenceKeys">依赖的缓存键</param>
        /// <returns></returns>
        public bool AddData(string key, object value, TimeSpan slidingExpiration, string[] dependenceKeys = null)
        {
            ExceptionFactory.StringNullorEmptyException(key);
            ExceptionFactory.ObjectNullException(value);

            CacheDependency cacheDepends = null;
            if (dependenceKeys != null && dependenceKeys.Length != 0)
            {
                cacheDepends = new CacheDependency(null, dependenceKeys);
            }

            _cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, slidingExpiration);
            return true;
        }

        /// <summary>
        /// 删除缓存对象
        /// </summary>
        /// <param name="key">指定对象的key值</param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            ExceptionFactory.StringNullorEmptyException(key);

            _cache.Remove(key);
            return true;
        }


    }
}
